const imgUrl = "https://useradmin.opos.tw";
const imgUrlShop = "https://bossadmin.opos.tw"
// const imgUrl = "https://taiwanyh.hujiaa.com";
// const imgUrlShop = "https://taiwansj.hujiaa.com";
const httpRequest = require("@/common/httpRequest.js")
// 時間格式化
const secondDate = (data, type, isMillis) => {
	let time = new Date(data * 1000);
	time = isMillis ? new Date(data) : new Date(data * 1000);
	let year = time.getFullYear();
	let month = time.getMonth() + 1;
	month = month < 10 ? '0' + month : month
	let day = time.getDate();
	day = day < 10 ? '0' + day : day
	let hours = time.getHours();
	hours = hours < 10 ? '0' + hours : hours
	let minute = time.getMinutes();
	minute = minute < 10 ? '0' + minute : minute
	let second = time.getSeconds();
	second = second < 10 ? '0' + second : second;
	if (type) {
		return year + '-' + month + '-' + day
	} else {
		return year + '-' + month + '-' + day + ' ' + hours + ':' + minute + ':' + second
	}
}
// 毫秒转秒
const millisecond = (second) => {
	return Math.floor(second / 1000)
}
const callphone = (p) => {
	uni.makePhoneCall({
		phoneNumber: p.toString()
	}).catch((e) => {
		// console.log(e)  //用catch(e)来捕獲錯误{makePhoneCall:fail cancel}
	})
}
const uploadImg = (upImgs, upImgs_arr, value) => {
	let _this = this;
	let num = Number(value) - upImgs.length;
	if (num <= 0) {
		return false;
	}
	uni.showActionSheet({
		itemList: ['照機', '相簿'],
		success: function(res) {
			// 選擇照機走0
			if (res.tapIndex == 0) {
				httpRequest.chooseImage(num, ['camera']).then(res => {
					for (var i = 0; i < res.length; i++) {
						uploadImageService(res[i], upImgs, upImgs_arr)
					}
				}).catch(err => {});
			}
			// 選擇相簿走1
			if (res.tapIndex == 1) {
				httpRequest.chooseImage(num, ['album']).then(res => {
					for (var i = 0; i < res.length; i++) {
						uploadImageService(res[i], upImgs, upImgs_arr)
					}
				}).catch(err => {});
			}
		},
		fail: function(res) {}
	});
}

const uploadImageService = (url, upImg, upImgs_arr) => {
	let _this = this;
	httpRequest.uploadFile('/api/common/upload', url).then(res => {
		upImg.push(res.url)
		let image_item = res.fullurl;
		upImgs_arr.push(image_item);
		console.log(upImg, "upImg")
		console.log(upImgs_arr, "upImgs_arr")
	}).catch(err => {});
}
// 購物車
const getCartList = async (shop_id) => {
	let res = await httpRequest.request('/api/goods/shopGoodsCartIndex', 'GET', {
		shop_id
	})
	if (res.code == 1) {
		let total_price = 0
		res.data.forEach(item => {
			total_price = Number(item.price)+Number(total_price)
		})
		return {
			total_price: total_price,
			total: res.data.length
		}
	}
}
export default {
	install: (Vue, options) => {
		Vue.prototype.imgUrl = imgUrl;
		Vue.prototype.imgUrlShop = imgUrlShop;
		Vue.prototype.secondDate = secondDate
		Vue.prototype.millisecond = millisecond
		Vue.prototype.uploadImg = uploadImg;
		Vue.prototype.callphone = callphone;
		Vue.prototype.getCartList = getCartList;
		
	}
}